class Member:
    """会员类，包含会员等级和计算折扣率"""
    DISCOUNT_RATES = {
        "普通会员": 0.95,  # 95折
        "银卡会员": 0.9,  # 9折
        "金卡会员": 0.85,  # 85折
        "钻石会员": 0.8  # 8折
    }

    def __init__(self, level="普通会员"):
        self.level = level if level in self.DISCOUNT_RATES else "普通会员"

    @property
    def discount_rate(self):
        """获取当前会员的折扣率"""
        return self.DISCOUNT_RATES[self.level]

    def __str__(self):
        return f"{self.level}（{self.DISCOUNT_RATES[self.level] * 100}%）"


class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price


class CartItem:
    def __init__(self, product, qty, member=None):
        self.product = product
        self.qty = qty
        self.member = member or Member()  # 默认普通会员

    def subtotal(self):
        """计算小计"""
        return self.product.price * self.qty

    def discounted_subtotal(self):
        """计算会员折扣后的金额"""
        return self.subtotal() * self.member.discount_rate


class ShoppingCart:
    def __init__(self):
        self.items = []

    def add(self, product, qty=1, member=None):
        """添加商品到购物车"""
        # 如果购物车中已有相同商品且会员相同，则更新数量
        for item in self.items:
            if item.product == product and item.member == member:
                item.qty += qty
                return
        # 添加新购物项
        self.items.append(CartItem(product, qty, member))

    def total(self):
        """计算购物车总金额（不考虑折扣）"""
        return sum(item.subtotal() for item in self.items)

    def apply_discount(self):
        """应用会员折扣，计算折扣后总金额"""
        return sum(item.discounted_subtotal() for item in self.items)

    def show(self):
        """显示购物车内容和折扣信息"""
        print("-" * 60)
        print(f"{'商品名称':<15}{'数量':<8}{'单价':<8}{'小计':<12}{'会员':<15}{'折扣小计':<12}")
        print("-" * 60)

        for item in self.items:
            print(
                f"{item.product.name:<15}{item.qty:<8}{item.product.price:<8.2f}¥{item.subtotal():<11.2f}{str(item.member):<15}¥{item.discounted_subtotal():<10.2f}")

        print("-" * 60)
        print(f"{'总计':<15}{'':<8}{'':<8}{'':<8}{'':<15}¥{self.total():<10.2f}")
        print(f"{'会员折扣后总价':<15}{'':<8}{'':<8}{'':<8}{'':<15}¥{self.apply_discount():<10.2f}")
        print(f"{'节省金额':<15}{'':<8}{'':<8}{'':<8}{'':<15}¥{(self.total() - self.apply_discount()):<10.2f}")


# --- 测试 ---
if __name__ == "__main__":
    # 创建不同等级的会员
    common_member = Member("普通会员")
    silver_member = Member("银卡会员")
    gold_member = Member("金卡会员")
    diamond_member = Member("钻石会员")

    # 创建商品
    apple = Product("苹果", 3.5)
    bread = Product("面包", 4.0)
    milk = Product("牛奶", 10.5)
    eggs = Product("鸡蛋", 5.0)

    # 创建购物车
    cart = ShoppingCart()

    # 添加商品到购物车
    cart.add(apple, 4, common_member)
    cart.add(bread, 2, silver_member)
    cart.add(milk, 1, gold_member)
    cart.add(eggs, 12, diamond_member)

    # 显示购物车内容和折扣信息
    cart.show()
